<template>
  <div style="width: 50%">
    <h2>供应商添加</h2>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="供应商编号:" prop="supplierCode">
            <el-input v-model="ruleForm.supplierCode" disabled></el-input>
          </el-form-item>

          <el-form-item label="供应商名称:" prop="supplierName">
            <el-input v-model="ruleForm.supplierName"></el-input>
          </el-form-item>

          <el-form-item label="类型:" prop="supplierType">
            <el-input v-model="ruleForm.supplierType"></el-input>
          </el-form-item>

          <el-form-item label="电话:" prop="phone">
            <el-input v-model="ruleForm.phone"></el-input>
          </el-form-item>

          <el-form-item label="传真:" prop="fax">
            <el-input v-model="ruleForm.fax"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="Email:" prop="email">
            <el-input v-model="ruleForm.email"></el-input>
          </el-form-item>

          <el-form-item label="联系人:" prop="contacts">
            <el-input v-model="ruleForm.contacts"></el-input>
          </el-form-item>

          <el-form-item label="地址:" prop="address">
            <el-input v-model="ruleForm.address"></el-input>
          </el-form-item>

          <el-form-item label="创建时间:" prop="createTime">
            <el-date-picker v-model="ruleForm.createTime" type="date" placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="FH()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { SupplierManagementDtoAddSave } from "@/api/BasicsDatumServer/BasicsDatumServer";
import { autoGenerateCode } from "@/utils/utils";
export default {
  data() {
    return {
      ruleForm: {
        supplierCode: ""
      },
      rules: {
        supplierName: [
          { required: true, message: "请输入供应商名称", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入电话", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        fax: [{ required: true, message: "请输入传真", trigger: "blur" }],
        contacts: [
          { required: true, message: "请输入联系人", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "请先选择日期", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.ruleForm.supplierCode = autoGenerateCode("DW");
    console.log(this.ruleForm.supplierCode)
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //添加
          SupplierManagementDtoAddSave(this.ruleForm).then((res) => {
            if (res.data.data > 0) {
              this.$message({
                message: "添加成功",
                type: "success",
              });
              this.$router.push("SupplierManagementList");
            } else {
              this.$message({
                message: "添加失败",
                type: "error",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    FH() {
      this.$router.push("SupplierManagementList");
    },
  },
};
</script>

<style></style>